<?php
/**
 * Created by JetBrains PhpStorm.
 * User: porterzhang
 * Date: 13-3-5 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        global $_G;

        //判断是否缺少参数
        if (empty($_GET['username']) || empty($_GET['caid'])) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        //得到登录用户信息

        $manager = C::t('#cloud#cloud_app_manage')->fetch_first_by_name($_GET['username'], $_GET['caid']);

        //设置用户登录状态
        //1.得到应用
        $app = C::t('#cloud#cloud_app')->fetch($manager['caid']);
        $manager['app'] = $app;

        $rolelist = array();
        $sysallList=array();
        if ($_GET['caid'] == 3) {
            $sysfunction = C::t('#cloud#cloud_function')->function_app_all_ftype_find();
            $sysallList = $sysfunction;
        } else {
            //判断是否是系统所建的用户角色组
            $sysfunction = C::t('#cloud#cloud_app_role')->fetch_app_role_info($manager['carid']);

            if (!empty($sysfunction)) {
                if ($sysfunction['createtype'] == 0) {
                    $roleSysArray = C::t('#cloud#cloud_role_purview')->role_purview_all_sys($manager['carid']);
                    $rolelist = $roleSysArray;
                }
                //得到管理员的权限组，取得本角色的所有功能
                $rolePurArray = C::t('#cloud#cloud_role_purview')->role_purview_all_not_lime($manager['carid'], $_GET['cadid']);
                foreach ($rolePurArray as $key => $val) {
                    array_push($rolelist, $val);
                }
            }
        }

$rolelists = array();
if(!empty($sysallList)){
 foreach($sysallList as $key => $val){
         $val['text'] = $val['name'];
                $val['id']=$val['linkurl'];
                $val['leaf'] = true;
         $sysallList[$key]=$val;
         $rolelists=$sysallList;
      }
}else{         
        foreach($rolelist as $key => $val){
            if( $val['fpid']!=null && $val['fpid']!=0){
                $val['text'] = $val['name'];
                $val['id'] = $val['linkurl'];
                $val['leaf'] = true;

                if(array_key_exists($val['fpid'], $rolelists) && !empty($rolelists[$val['fpid']]['items'])){
                    array_push($rolelists[$val['fpid']]['Variables']['data'],  $val);
                }else{
                    $rolelists[$val['fpid']]['Variables']['data'][]=$val;
                }
                unset($rolelists[$val['fpid']]['leaf']);
            }else{ 
                $val['text'] = $val['name'];
                $val['id']=$val['linkurl'];
                $val['leaf'] = true;
                if($val['fid']!=null){                    
                    $rolelists[$val['fid']]= $val;
                }else{
                    $rolelists[$val['id']]= $val;
                }

            }

        }
} 


        $variable = array(
            'data' => array_values($rolelists),
            'messagestr' => lang('plugin/cloud', '1013'),
        );

        cloud_core::result(cloud_core::variable($variable));
    }


    /**
     * 返回错误信息
     * @param $code
     */
    function setMessage($code) {
        $variable = array(
            'code' => $code,
            'messagestr' => lang('plugin/cloud', '' . $code),
        );
        cloud_core::result(cloud_core::variable($variable));
    }

}

?>

